<!--
 * Description: 报价系列修改页面
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Qiuxue.Wu
 * Date: 2020/05/22 09:33
-->

<template>
  <div>
    <!--数据表单-->
    <el-form label-width="120px" :model="formData" size="mini">
      <el-row>
        <!--行号-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.NO')">
            <span>{{ currentRowNo }}</span>
          </el-form-item>
        </el-col>

        <!--RD Function-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.rdFunction')">
            <span>{{ formData.rdFunction }}</span>
          </el-form-item>
        </el-col>

        <!--Category-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.category')">
            <span>{{ formData.category }}</span>
          </el-form-item>
        </el-col>

        <!--GCM Function-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.gcmFunction')">
            <el-select
              v-model="formData.gcmFunction"
              clearable
              filterable
              :placeholder="$t('select.placeholder')"
              @change="changeGcmFunction"
            >
              <el-option
                v-for="item in $store.getters['enums/getGcmFunctionListOnly']"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <!--采购分类-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.commodity')">
            <el-select
              v-model="formData.commodity"
              clearable
              filterable
              :placeholder="$t('select.placeholder')"
              size="mini"
              style="width: 100%"
            >
              <el-option
                v-for="item in $store.getters['commodity/getCommodityCollectsByGcmFunction'](
                  formData.gcmFunction
                )"
                :key="item.name"
                :label="item.name"
                :value="item.name"
              >
                <span style="float: left;">{{ item.name }}</span>
                <span style="float: right; color: #b4b4b4; font-size: 12px">
                  {{ item.gcmFunction }}
                </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <!--GCM Owner-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.gcmOwner')">
            <span v-if="formData.gcmOwner">
              {{ $store.getters['user/getUserNameById'](formData.gcmOwner) || $t('result.none') }}
            </span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span style="display: block;text-align: center;margin-top: 5px">
      <!--取消按钮-->
      <el-button size="medium" @click="cancel">{{ $t('button.cancel') }}</el-button>
      <!--保存按钮-->
      <el-button size="medium" type="primary" @click="save">{{ $t('button.save') }}</el-button>
    </span>
  </div>
</template>

<script>
import { fdAssignSaveSingel } from '@/api/quote/fd'

export default {
  name: 'FdAssignEdit',
  props: {
    inputData: {
      type: Object,
      default: () => {}
    },
    currentRowNo: {
      type: Number,
      default: 0
    },
    fdBaseData: {
      type: Object,
      default: () => []
    }
  },
  data() {
    return {
      formData: {}
    }
  },
  mounted() {
    this.pageDataInit()
  },
  methods: {
    pageDataInit() {
      // 将props传入的值复制给formData的值
      this.formData = this.$deepCopy(this.inputData)
    },
    async save() {
      try {
        await fdAssignSaveSingel(
          {
            commodity: this.formData.commodity,
            fdRowId: this.formData.fdRowId,
            gcmFunction: this.formData.gcmFunction,
            gcmOwner: this.formData.gcmOwner
          },
          { msgSuccess: true, msgError: true }
        )
        this.$emit('save', this.formData)
      } catch (error) {
        console.log(error)
      }
    },
    cancel() {
      this.$emit('cancel')
    },
    changeGcmFunction(val) {
      this.formData.gcmOwner = this.fdBaseData[val.toLowerCase() + 'Owner']
      this.formData.commodity = ''
    }
  }
}
</script>

<style scoped>
.el-form-item__content span {
  margin-left: 5px;
  color: blue;
}
</style>
